<?php

class mysql{
     private $host;
     private $username;
     private $password;
     private $database;
     private $charset;
     protected static $link;
     private $table;
     protected $error;
     private $insert_id;

     public function __construct($conf){
     	$this->host=$conf['host'];
     	$this->username=$conf['name'];
     	$this->password=$conf['password'];
     	$this->database=$conf['database'];
          $this->charset=$conf['charset'];
     	if(is_null(self::$link)){
           $this->connect();
     	}
          $this->setcharset($this->charset);
     }


     function connect(){
		self::$link=mysqli_connect($this->host,$this->username,$this->password,$this->database);

		if(!self::$link){
           die('连接失败:'.$this->getMessage($this->mysqli_connect_error()));
		}
     }

     function getMessage($meg){
          echo $meg;
     }


     //设置字符集
     function setcharset($zfj){
      $sql='set names '.$zfj;
      $this->query($sql);
     }

     //设置查询
     function query($sql){
     	return mysqli_query(self::$link,$sql);
     }



     //查询表中全部数据
     function getAll($table){

     	$sql="select * from ".$table;
     	$res=$this->query($sql);
     	if($res){
     		while($date=mysqli_fetch_assoc($res)) {
     			$ress[]=$date;
     		}
     		return $ress;
     	}
     }

     //根据条件查询表中全部数据
     function getCondition($sql){
        $res=$this->query($sql);
        $ress=array();
        if($res){
           while ($date=mysqli_fetch_assoc($res)) {
               $ress[]=$date;
           }

        }return $ress;
     }







     //查询表中单条记录
     function getOne($sql){
         $res=$this->query($sql);
         $date=mysqli_fetch_assoc($res);
         if($date){
            return $date;
         }else{
            return false;
         }
     }



     function insert($arr,$table){
			$key="`".join("`,`",array_keys($arr))."`";  //将键值连接
			$value="'".join("','",array_values($arr))."'";
			$sql="insert into `{$table}`({$key}) values({$value})";
            // echo $sql;die;
			$result=$this->query($sql);
            $this->insert_id=mysqli_insert_id(self::$link);
		     // return mysqli_insert_id(self::$link);
            return mysqli_affected_rows(self::$link);
     }

     function getInsertid(){
        return $this->insert_id;
     }



	function update($arr,$where,$table){
		 foreach ($arr as $k => $v) {
			$str[]='`'.$k."`='".$v."'";
		 }
         foreach ($where as $k => $v) {
                 $condition[]='`'.$k."`=".$v;
         }


		$strs=implode(',',$str);
		$conditions=implode(' and ',$condition);
		$sql="update {$table} set {$strs} where {$conditions}";
        // echo $sql;die;
		$this->query($sql);


        return mysqli_affected_rows(self::$link);
	}


	function delete($where,$table){
          //如果$where是数组则用，拆分
         if(is_array($where)){
            $where='in ('.implode(',',$where).')';
         }else{
            $where="=".$where;
         }

         $sql="delete from ".$table." where id ".$where;
		 $this->query($sql);
         return mysqli_affected_rows(self::$link);

	}


    function del($where,$table){
     $sql="delete from ".$table." where ".$where;
     // echo $sql;die;
     $this->query($sql);
     return mysqli_affected_rows(self::$link);
    }



}




?>